#include"StdAfx.h"

pgMesh *pgOBJMeshLoader::LoadMesh(const char* szFilePath)
{
	PG_LOG_INFO_EX(TOPIC_OBJLOADER, "Loading mesh from file: %s!", szFilePath);
	pgFile objFile;

	if (!objFile.OpenFile(szFilePath))
		return NULL;

	std::string fileBuffer = objFile.GetTextBuffer();

	pgMesh *pMesh = new NEW_MEM_LEAK pgMesh();



	// Count verts
	size_t pos = 0;
	int vertexCount = 0;
	while (true)
	{
		pos = fileBuffer.find("\nv", ++pos);
		if (pos != -1)
			++vertexCount;
		else
			break;
	}
	// Allocate memory
	SVertex *pVerts = new NEW_MEM_LEAK SVertex[vertexCount];
	pos = 0;

	// Do some magic
	int vertexIndex = 0;
	size_t last = 0;
	do
	{
		pos = fileBuffer.find("\nv", ++last);

		if (pos != -1)
		{
			size_t endPos = fileBuffer.find("\n", pos + 1);

			pVerts[vertexIndex].pos = ExtractVectorFromLine(fileBuffer.substr(pos, endPos - pos));
			last = ++pos;
			++vertexIndex;
		}
	}while (pos != -1);

	pMesh->SetVertices(pVerts, vertexCount);

	PG_LOG_INFO_EX(TOPIC_OBJLOADER, "Finished reading vertices! Counted: %d!", vertexCount);

	// Index

	pos = 0;
	size_t lastLine = 0;
	int indexCount = 0;
	while (true)
	{
		pos = fileBuffer.find("\nf", ++pos);
		if (pos != -1)
		{
			lastLine = pos;
			++indexCount;
		}
		else
			break;
	}

	// three per face
	indexCount *= 3;

	// allocate space
	WORD *pIndices = new NEW_MEM_LEAK WORD[indexCount];

	/*size_t indexStart = fileBuffer.find("\nf");
	size_t indexLast = fileBuffer.find("\n", ++lastLine);
	
	std::string indexString = fileBuffer.substr(indexStart, indexLast - indexStart);

	bool bLastWasDigit = true;
	int currentIndex = 0;
	std::string	currentDigit;
	for (int i = 0; i < indexString.length(); ++i)
	{
		if (isdigit(indexString[i]))
		{
			currentDigit += indexString[i];

			if (bLastWasDigit == false)
			{
				pIndices[currentIndex] = atoi(currentDigit.c_str());
				currentDigit = "";
				++currentIndex;
			}
	
			bLastWasDigit = true;
		}
		else
		{
			bLastWasDigit = false;
		}
	}*/

	 pos = 0;
	size_t endPos = 0;

	int indexIndex = 0;

	while (pos != -1)
	{
		pos = fileBuffer.find("\nf", endPos);

		if (pos == -1)
			break;

		endPos = fileBuffer.find("\n", pos + 1);

		XMFLOAT3 temp = ExtractVectorFromLine(fileBuffer.substr(pos, endPos - pos));

		pIndices[indexIndex] = temp.x;
		++indexIndex;
		pIndices[indexIndex] = temp.y;
		++indexIndex;
		pIndices[indexIndex] = temp.z;
		++indexIndex;
	}

	pMesh->SetIndices(pIndices, indexCount);

	PG_LOG_INFO_EX(TOPIC_OBJLOADER, "Finished reading indices! Counted: %d!", indexCount);
	PG_LOG_INFO_EX(TOPIC_OBJLOADER, "Finished loading mesh from file: %s!", szFilePath);

	return pMesh;
}

XMFLOAT3 pgOBJMeshLoader::ExtractVectorFromLine(std::string &sLine)
{
	size_t pos1 = sLine.find_first_of(" ");
	size_t pos2 = sLine.find(" ", ++pos1 + 1);
	size_t pos3 = sLine.find(" ", ++pos2);

	XMFLOAT3 result;

	result.x = atof(sLine.substr(pos1, pos2 - pos1).c_str());
	result.y = atof(sLine.substr(pos2, pos3 - pos2).c_str());
	result.z = atof(sLine.substr(pos3, sLine.length() - pos3).c_str());

	return result;
}